//
//  ZYSTableViewCell.m
//  budejie
//
//  Created by oldSix_Zhu on 16/8/27.
//  Copyright © 2016年 oldSix_Zhu. All rights reserved.
//

#import "ZYSTableViewCell.h"
#import <UIImageView+WebCache.h>


@interface ZYSTableViewCell ()
//头像
@property (weak, nonatomic) IBOutlet UIImageView *uHeaderImg;
//名字
@property (weak, nonatomic) IBOutlet UILabel *uName;
//时间
@property (weak, nonatomic) IBOutlet UILabel *passtimeLbl;

//内容
@property (weak, nonatomic) IBOutlet UILabel *textLbl;
//内容高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textHeight;

//图片
@property (weak, nonatomic) IBOutlet UIImageView *bigImg;
//图片高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgHeight;

//标签
@property (weak, nonatomic) IBOutlet UILabel *tagLbl;

//赞
@property (weak, nonatomic) IBOutlet UIButton *upBtn;
//踩
@property (weak, nonatomic) IBOutlet UIButton *downBtn;
//转发
@property (weak, nonatomic) IBOutlet UIButton *forwardBtn;
//评论
@property (weak, nonatomic) IBOutlet UIButton *commentBtn;

//vip
@property (weak, nonatomic) IBOutlet UIImageView *vipImg;


@end

@implementation ZYSTableViewCell
-(void)awakeFromNib
{
    self.uHeaderImg.layer.cornerRadius = self.uHeaderImg.bounds.size.width * 0.5;
    
    self.uHeaderImg.layer.masksToBounds = YES;
    
//    //创建手势
//    UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//    
//    tag.numberOfTapsRequired = 1;
//    tag.numberOfTouchesRequired = 1;
//    
//    [self.uHeaderImg addGestureRecognizer:tag];
}




-(void)setModel:(ZYSCellModels *)model
{
    _model = model;
    
    //头像
    NSString *header = [NSString stringWithFormat:@"%@",[model.u[@"header"] lastObject]];
    
    [self.uHeaderImg sd_setImageWithURL:[NSURL URLWithString:header]];
    
    //是否vip
    BOOL vip = model.u[@"is_vip"];
    
    if (!vip)
    {
        self.vipImg.hidden = YES;
    }
    
    //名字
    self.uName.text = model.u[@"name"];
    //时间
    self.passtimeLbl.text = model.passtime;
    //文本
    self.textLbl.text = model.text;
    
    NSDictionary *textDic = @{
                              NSFontAttributeName:[UIFont systemFontOfSize:17],
                              };
    
    CGSize textSize = [model.text boundingRectWithSize:self.textLbl.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:textDic context:nil].size;
    
    self.textHeight.constant = textSize.height;
    
    
    NSDictionary *image = model.image;
    NSDictionary *gif = model.gif;
//    NSDictionary *video = model.video;
    //图片
    if ([model.type isEqualToString:@"image"])
    {
        NSLog(@"图片");
        self.bigImg.hidden = NO;
        NSNumber *height = image[@"height"];
        NSNumber *width = image[@"width"];
        CGFloat caculateHeight = height.floatValue / width.floatValue * self.bigImg.bounds.size.width;
        
        //大图判st断
        self.imgHeight.constant = (caculateHeight > 1000) ? 400 : caculateHeight;
        
        [self.bigImg sd_setImageWithURL:[NSURL URLWithString:image[@"big"][0]]];
    }
    //文本
    else if ([model.type isEqualToString:@""])
    {
        NSLog(@"文本");
        self.bigImg.hidden = YES;
        self.bigImg.image = nil;
        self.imgHeight.constant = 0;
        
    }
    //gif
    else if ([model.type isEqualToString:@"gif"])
    {
        NSLog(@"gif");
        self.bigImg.hidden = NO;
        
        NSNumber *height = gif[@"height"];
        NSNumber *width = gif[@"width"];
        
        self.imgHeight.constant = height.floatValue / width.floatValue * self.bigImg.bounds.size.width;
        
        [self.bigImg sd_setImageWithURL:[NSURL URLWithString:gif[@"images"][0]]];
        
    }
    //视频
    else
    {
        NSLog(@"视频");
        self.bigImg.hidden = YES;
        self.bigImg.image = nil;
        self.imgHeight.constant = 0;
        
    }
    
    
    //标签
    NSArray *tagsArr = model.tags;
    NSMutableString *tagStr = [NSMutableString string];
    for ( NSDictionary *dic in tagsArr)
    {
        [tagStr appendFormat:@" %@ ",dic[@"name"]];
    }
    self.tagLbl.text = tagStr.copy;
    
    //顶数
    self.upBtn.titleLabel.text = model.up;
    //踩数
    self.downBtn.titleLabel.text = [NSString stringWithFormat:@"%@",model.down];
    //转发数
    self.forwardBtn.titleLabel.text =[NSString stringWithFormat:@"%@",model.forward];
    //评论数
    self.commentBtn.titleLabel.text = [NSString stringWithFormat:@"%@",model.comment];
    
    //行高
    self.cellHeight = self.textHeight.constant + self.imgHeight.constant + 200;
    
}

//右上角更多
- (IBAction)didClickMore:(id)sender
{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"收藏" otherButtonTitles:@"举报", nil];
    
    [sheet showInView:self];

}

//赞
- (IBAction)upBtn:(UIButton *)sender
{
    [self didClickBtn:sender andImgName:@"mainCellDingClick"];
}

//踩
- (IBAction)downBtn:(UIButton *)sender
{
    [self didClickBtn:sender andImgName:@"mainCellCaiClick"];
    
}

//点击
-(void)didClickBtn:(UIButton *)sender andImgName:(NSString *)imgName
{
    [sender setImage:[UIImage imageNamed:imgName] forState:UIControlStateSelected];
    sender.selected = YES;
    
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    
    self.upBtn.userInteractionEnabled = NO;
    self.downBtn.userInteractionEnabled = NO;
    
}


//转发
- (IBAction)forwardBtn:(UIButton *)sender
{
    
}

//评论
- (IBAction)wordBtn:(UIButton *)sender
{
    
}

#pragma mark - 点击了头像的点击事件

//- (void)tapAction:(UITapGestureRecognizer *)tag
//{
//    NSLog(@"hahaha");
//}
//

@end


